import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-18
 * Time: 17:44
 */
public class MyQueue {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    public MyQueue() {

    }

    public void push(int x) {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            stack1.push(x);
        }else if (!stack1.isEmpty()) {
            stack1.push(x);
        }else {
            stack2.push(x);
        }
    }

    public int pop() {
        if (!stack1.isEmpty() && stack2.isEmpty()) {
            for (int i = 0; i < stack1.size()-1; i++) {
                stack2.push(stack1.pop());
            }
            return stack1.pop();
        }else {
            for (int i = 0; i < stack2.size()-1; i++) {
                stack1.push(stack2.pop());
            }
            return stack2.pop();
        }
    }

    public int peek() {
        if (!stack1.isEmpty() && stack2.isEmpty()) {
            for (int i = 0; i < stack1.size()-1; i++) {
                stack2.push(stack1.pop());
            }
            int ans =  stack1.peek();
            for (int i = 0; i < stack2.size(); i++) {
                stack1.push(stack2.pop());
            }
            return ans;
        }else {
            for (int i = 0; i < stack2.size()-1;i++) {
                stack1.push(stack2.pop());
            }
            int ans = stack2.peek();
            for (int i = 0; i < stack1.size(); i++) {
                stack2.push(stack1.pop());
            }
            return ans;
        }
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}
